package com.pomr.controller.simstorage;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.model.equipmentstorage.EquipmentInStorage;
import com.pomr.model.menu.NetworkOperator;
import com.pomr.model.simstorage.SimInStorage;
import com.pomr.model.synthesize.Equipment;
import com.pomr.model.synthesize.Manufacturer;
import com.pomr.model.sys.Agency;
import com.pomr.model.sys.Province;
import com.pomr.model.sys.User;
import com.pomr.service.simstorage.SimInStorageService;
import com.pomr.service.synthesize.EquipmentService;
import com.pomr.service.synthesize.ManufacturerService;
import com.pomr.service.sys.AgencyService;
import com.pomr.service.sys.UserService;
import com.pomr.util.common.CommonData;
import com.pomr.util.common.JWT;
import com.pomr.util.common.ResponseData;
import com.pomr.util.page.Page;
import com.pomr.util.page.PageParam;
/**
 * sim卡入库
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/simInStorage")
public class SimInStorageController {
	@Autowired
	@Qualifier("simInStorageServiceImpl")
	private SimInStorageService simInStorageService;
	@Autowired
	@Qualifier("agencyServiceImpl")
	private AgencyService agencyService;
	@Autowired
	@Qualifier("equipmentServiceImpl")
	private EquipmentService equipmentService;
	@Autowired
	@Qualifier("userServiceImpl")
	private UserService userService;
	@Autowired
	@Qualifier("manufacturerServiceImpl")
	private ManufacturerService manufacturerService;
	
	/**
	 * 入库前查询信息（网络运营商、机构）
	 * @param request
	 * @return
	 */
	@RequestMapping("inter-addbefore")
	@ResponseBody
	public ResponseData addStorageBefore(HttpServletRequest request){
		ResponseData pd = null;
		try {
			User user = JWT.findUserByToken(request);
			List<Agency> agencys = agencyService.searchAgencysOfUser(user);
			pd = ResponseData.ok();
			if(agencys == null || agencys.size()<=0){
				pd = ResponseData.custom("未找到可以入库的机构");
			}else{
				pd.putDataValue("agencys", agencys);
				//获取网络运营商
				List<NetworkOperator> networks = (List<NetworkOperator>) CommonData.data.get("NetworkOperator");
				pd.putDataValue("networks", networks);
				//获取当前机构的人员
				List<User> users = userService.searchUsersOfAgency(user);
				pd.putDataValue("users", users);
				//获取厂商信息
				List<Manufacturer> manufacturers = manufacturerService.searchManufacturersOfAgency(user);
				pd.putDataValue("manufacturers", manufacturers);
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 设备入库
	 * @param manufacturer
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-add",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData simInStorage(@RequestBody SimInStorage instor,HttpServletRequest request){
		ResponseData pd = null;
		try{
			User user = JWT.findUserByToken(request);
			instor.setCreateUser(user);
			instor.setCreateTime(new Date());
			//TODO 判断数量与编码数是否相符
			if(simInStorageService.inStorage(instor)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 查询SIM卡入库信息  （网络运营商、机构、设备、入库日期）
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-search",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<SimInStorage>> searchSimInStorages(PageParam param){
		try{
			String page = param.getPage();
			String limit = param.getLimit();
			Map<String, String> map = param.getParams();
			String networkid = map.get("networkid");
			String agencyid = map.get("agencyid");
			String manufacturerid = map.get("manufacturerid");
			String time1 = map.get("time1");
			String time2 = map.get("time2");
			return simInStorageService.searchSimInStorages(networkid, agencyid, manufacturerid, time1, time2, page, limit);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * SIM卡入库记录详细信息
	 * @param manufacturer
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-searchone",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData searchSimInStorage(@RequestBody SimInStorage instor){
		ResponseData pd = null;
		try{
			instor = simInStorageService.searchSimInStorageById(instor);
			if(instor!=null){
				pd = ResponseData.ok();
				pd.putDataValue("storage", instor);
			}else{
				pd = ResponseData.notFound();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 删除入库记录--如果库中信息已使用 不能删除
	 * @param map id:入库记录主键  del：（1更新库存记录，0：）
	 * @return
	 */
	@RequestMapping(value="/inter-delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteSimInStorage(@RequestBody Map<String,String> map) {
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			String del = map.get("del");
			if(simInStorageService.deleteSimInStorageById(ids.split(","),del)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 审核入库记录
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inter-review",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData reviewSimInStorage(@RequestBody Map<String,String> map) throws Exception{
		ResponseData pd = null;
		String ids = map.get("id");
		if(simInStorageService.reviewSimInStorageById(ids.split(","))){
			pd = ResponseData.ok();
		}else{
			pd = ResponseData.custom("审核失败");
		}
		return pd;
	}

}
